//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc, Kasper og Nikolaj
//*****************************************************************************
package view;

import control.ControlUtil;
import control.PersonaleHandler;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import model.*;
import org.hibernate.NonUniqueObjectException;

public class FagAdminPanel extends javax.swing.JPanel {

    private ControlUtil controlUtil;
    private PersonaleHandler personaleControl;
    private DefaultListModel holdetsFagModel;
    private DefaultListModel tilgaengeligeFagModel;

    public FagAdminPanel(ControlUtil controlUtil) {
        this.controlUtil = controlUtil;
        holdetsFagModel = new DefaultListModel();
        initComponents();
        henttHold();
        indsaetLærerer();
        infoArea.setEnabled(false);
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        opdelingCombo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        nytFag = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        vaelgHoldCombo = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList(holdetsFagModel);
        jScrollPane1 = new javax.swing.JScrollPane();
        infoArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        opretHoldFag = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        klassetrinCombo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        vaelgFag = new javax.swing.JComboBox();
        laererCombo = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        holdCombo = new javax.swing.JComboBox();

        setLayout(null);

        jButton2.setText("Slet fag");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2);
        jButton2.setBounds(50, 440, 80, 20);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Nyt fag"));
        jPanel1.setLayout(null);

        jButton3.setText("Opret fag");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(90, 170, 120, 40);

        jLabel6.setText("Opdeling:");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(20, 80, 80, 14);

        opdelingCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg Opdeling", "3 deling (1-3, 4-6, 7-9)", "10 deling(1,2,3,4 osv.)" }));
        opdelingCombo.setEnabled(false);
        opdelingCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                opdelingComboActionPerformed(evt);
            }
        });
        jPanel1.add(opdelingCombo);
        opdelingCombo.setBounds(100, 80, 110, 20);

        jLabel3.setText("Fag: ");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(20, 40, 70, 14);

        nytFag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nytFagActionPerformed(evt);
            }
        });
        nytFag.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nytFagKeyReleased(evt);
            }
        });
        jPanel1.add(nytFag);
        nytFag.setBounds(100, 40, 110, 20);

        add(jPanel1);
        jPanel1.setBounds(620, 20, 280, 220);

        jLabel2.setText("Hold:");
        add(jLabel2);
        jLabel2.setBounds(20, 20, 70, 14);

        vaelgHoldCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg Hold" }));
        vaelgHoldCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vaelgHoldComboActionPerformed(evt);
            }
        });
        add(vaelgHoldCombo);
        vaelgHoldCombo.setBounds(20, 40, 100, 20);

        jLabel7.setText("Holdets Fag:");
        add(jLabel7);
        jLabel7.setBounds(20, 80, 110, 14);

        jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList2ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList2);

        add(jScrollPane2);
        jScrollPane2.setBounds(20, 100, 190, 320);

        infoArea.setColumns(20);
        infoArea.setRows(5);
        jScrollPane1.setViewportView(infoArea);

        add(jScrollPane1);
        jScrollPane1.setBounds(220, 160, 280, 150);

        jLabel1.setText("Informationer om faget:");
        add(jLabel1);
        jLabel1.setBounds(220, 140, 160, 14);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Tilknyt fag"));
        jPanel2.setLayout(null);

        opretHoldFag.setText("Tilknyt fag");
        opretHoldFag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                opretHoldFagActionPerformed(evt);
            }
        });
        jPanel2.add(opretHoldFag);
        opretHoldFag.setBounds(80, 200, 120, 40);

        jLabel9.setText("Klassetrin:");
        jPanel2.add(jLabel9);
        jLabel9.setBounds(30, 130, 50, 14);

        klassetrinCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg Årgang", "0. Klasse", "1. Klasse", "2. Klasse", "3. Klasse", "4. Klasse", "5. Klasse", "6. Klasse", "7. Klasse", "8. Klasse", "9. Klasse", "10 . Klasse" }));
        klassetrinCombo.setEnabled(false);
        klassetrinCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                klassetrinComboActionPerformed(evt);
            }
        });
        jPanel2.add(klassetrinCombo);
        klassetrinCombo.setBounds(100, 130, 110, 20);

        jLabel4.setText("Fag: ");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(30, 90, 70, 14);

        vaelgFag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg fag" }));
        vaelgFag.setEnabled(false);
        vaelgFag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vaelgFagActionPerformed(evt);
            }
        });
        jPanel2.add(vaelgFag);
        vaelgFag.setBounds(100, 90, 110, 20);

        laererCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg Lærer" }));
        laererCombo.setEnabled(false);
        laererCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                laererComboActionPerformed(evt);
            }
        });
        jPanel2.add(laererCombo);
        laererCombo.setBounds(100, 170, 150, 20);

        jLabel10.setText("Vælg Lærer:");
        jPanel2.add(jLabel10);
        jLabel10.setBounds(20, 170, 70, 14);

        jLabel11.setText("Hold:");
        jPanel2.add(jLabel11);
        jLabel11.setBounds(60, 50, 40, 14);

        holdCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg Hold" }));
        holdCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                holdComboActionPerformed(evt);
            }
        });
        jPanel2.add(holdCombo);
        holdCombo.setBounds(100, 50, 100, 20);

        add(jPanel2);
        jPanel2.setBounds(620, 250, 280, 260);
    }// </editor-fold>//GEN-END:initComponents

    private void henttHold() {
        int value = vaelgHoldCombo.getSelectedIndex();
        Hold[] hold = controlUtil.getHoldHandler().getHold();
        vaelgHoldCombo.removeAllItems();
        vaelgHoldCombo.setEnabled(true);
        vaelgHoldCombo.addItem("Vælg Hold");
        for (int i = 0; i < hold.length; i++) {
            vaelgHoldCombo.addItem(hold[i]);
            holdCombo.addItem(hold[i]);
        }
        vaelgHoldCombo.setSelectedIndex(value);
    }

    private void opdaterHoldetsFag(Hold holdet) {
        holdetsFagModel.clear();
        HoldFag[] holdetsFag = controlUtil.getHoldHandler().getHoldFag(holdet);
        for (int i = 0; i < holdetsFag.length; i++) {
            holdetsFagModel.addElement(holdetsFag[i]);

        }
    }

    private void indsaetFag(Hold h) {
        vaelgFag.removeAllItems();
        vaelgFag.setEnabled(true);
        vaelgFag.addItem("Vælg fag");
        Fag[] fags = controlUtil.getFagHandler().hentFagIkkeHold(h);
        for (int i = 0; i < fags.length; i++) {
            vaelgFag.addItem(fags[i]);
        }
    }

    private void indsaetLærerer() {
        laererCombo.removeAllItems();
        laererCombo.addItem("Vælg lærer");
        Personale[] pers = controlUtil.getPersonaleHandler().hentPersonale(1);
        for (int i = 0; i < pers.length; i++) {
            laererCombo.addItem(pers[i]);
        }
    }

    private void opdelingComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opdelingComboActionPerformed
        if (opdelingCombo.getSelectedIndex() != 0) {


            jButton3.setEnabled(true);
        } else {
            jButton3.setEnabled(false);
        }
    }//GEN-LAST:event_opdelingComboActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        boolean opdeling = false;
        switch (opdelingCombo.getSelectedIndex()) {

            case 1:
                opdeling = true;
                break;
            case 0:
                opdeling = false;
                break;

        }


        Fag fag = new Fag(nytFag.getText());
        fag.setOpdeling(opdeling);
        try {


            controlUtil.getFagHandler().nytFag(fag);
        } catch (NonUniqueObjectException ex) {
            String text = "Det var ikke muligt og tilføje " + nytFag.getText() + ", Da det allerede findes!";
            JOptionPane.showMessageDialog(this, text, "", JOptionPane.ERROR_MESSAGE);
        }
        opdelingCombo.setSelectedIndex(1);
        nytFag.setText("");
        opdelingCombo.setEnabled(false);
        indsaetLærerer();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void vaelgHoldComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vaelgHoldComboActionPerformed
        if (vaelgHoldCombo.getSelectedIndex() != 0 && vaelgHoldCombo.getItemCount() != 0) {
            Hold holdet = (Hold) vaelgHoldCombo.getSelectedItem();
            opdaterHoldetsFag(holdet);
        } else {
            holdetsFagModel.clear();
        }
    }//GEN-LAST:event_vaelgHoldComboActionPerformed

    private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList2ValueChanged
        if (!jList2.isSelectionEmpty()) {
            HoldFag valgtFag = (HoldFag) holdetsFagModel.getElementAt(jList2.getSelectedIndex());
            infoArea.setText(fagInformation(valgtFag));
            jButton2.setEnabled(true);
        } else {
            infoArea.setText("");
        }
    }//GEN-LAST:event_jList2ValueChanged

    private void opretHoldFagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opretHoldFagActionPerformed
        if (vaelgFag.getSelectedIndex() != 0 && klassetrinCombo.getSelectedIndex() != 0
                && laererCombo.getSelectedIndex() != 0 && holdCombo.getSelectedIndex() != 0) {
            Fag f = (Fag) vaelgFag.getSelectedItem();
            Hold h = (Hold) holdCombo.getSelectedItem();
            Personale p = (Personale) laererCombo.getSelectedItem();
            controlUtil.getHoldHandler().opretHoldFag(new HoldFag(new HoldFagId(f.getFag(), h.getHoldId()), p, h, f, klassetrinCombo.getSelectedIndex() - 1 + ""));
            henttHold();
            opdaterHoldetsFag(h);
            vaelgHoldCombo.setSelectedItem(h);
            klassetrinCombo.setEnabled(false);
            klassetrinCombo.setSelectedIndex(0);

            holdCombo.setSelectedIndex(0);
            holdCombo.setEnabled(false);

            laererCombo.setSelectedIndex(0);
            laererCombo.setEnabled(false);


        }
    }//GEN-LAST:event_opretHoldFagActionPerformed

    private void klassetrinComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_klassetrinComboActionPerformed
        if (klassetrinCombo.getItemCount() != 0 && klassetrinCombo.getSelectedIndex() != 0) {
            laererCombo.setEnabled(true);
        } else {
            laererCombo.setEnabled(false);
        }
    }//GEN-LAST:event_klassetrinComboActionPerformed

    private void laererComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_laererComboActionPerformed
        if (laererCombo.getItemCount() != 0 && laererCombo.getSelectedIndex() != 0) {
            opretHoldFag.setEnabled(true);
        } else {
            opretHoldFag.setEnabled(false);
        }
    }//GEN-LAST:event_laererComboActionPerformed

    private void holdComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_holdComboActionPerformed
        if (holdCombo.getItemCount() != 0 && holdCombo.getSelectedIndex() != 0) {
            vaelgFag.setEnabled(true);
            indsaetFag((Hold) holdCombo.getSelectedItem());
        } else {
            vaelgFag.setEnabled(false);
        }
        }//GEN-LAST:event_holdComboActionPerformed

    private void vaelgFagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vaelgFagActionPerformed
        if (vaelgFag.getItemCount() != 0 && vaelgFag.getSelectedIndex() != 0) {
            Fag f = (Fag) vaelgFag.getSelectedItem();
            klassetrinCombo.removeAllItems();
            klassetrinCombo.setEnabled(true);
            klassetrinCombo.addItem("Vælg klassetrin");
            if (f.getOpdeling()) {
                klassetrinCombo.addItem("0-2. klasse");
                klassetrinCombo.addItem("3-5. klasse");
                klassetrinCombo.addItem("6-9. klasse");

            } else {
                klassetrinCombo.addItem("0. klasse");
                klassetrinCombo.addItem("1. klasse");
                klassetrinCombo.addItem("2. klasse");
                klassetrinCombo.addItem("3. klasse");
                klassetrinCombo.addItem("4. klasse");
                klassetrinCombo.addItem("5. klasse");
                klassetrinCombo.addItem("6. klasse");
                klassetrinCombo.addItem("7. klasse");
                klassetrinCombo.addItem("8. klasse");
                klassetrinCombo.addItem("9. klasse");
            }
            klassetrinCombo.setEnabled(true);
        } else {
            klassetrinCombo.setEnabled(false);
        }

    }//GEN-LAST:event_vaelgFagActionPerformed

    private void nytFagKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nytFagKeyReleased
        if (nytFag.getText().length() != 0) {
            opdelingCombo.setEnabled(true);
        } else {
            opdelingCombo.setEnabled(false);
        }
    }//GEN-LAST:event_nytFagKeyReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed


        HoldFag faget = (HoldFag) jList2.getSelectedValue();
        controlUtil.getHoldHandler().sletFag(faget);
        henttHold();
        Hold holdet = (Hold) vaelgHoldCombo.getSelectedItem();
        opdaterHoldetsFag(holdet);

    }//GEN-LAST:event_jButton2ActionPerformed

    private void nytFagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nytFagActionPerformed
    }//GEN-LAST:event_nytFagActionPerformed

    private String fagInformation(HoldFag faget) {

        String fagetsNavn = "Fag: " + faget.getFag().toString();
        String laerer = "Lærer: " + faget.getPersonale().toString();
        String trindeling;

        if (faget.getFag().getOpdeling()) {
            trindeling = "Faget opdeles i tre niveauer";
        } else {
            trindeling = "Faget opdeles i 10 niveauer";
        }

        String niveau = "Aktuelt niveau: " + faget.getKlassetrin().toString() + " klasse";

        String information = fagetsNavn + "\n" + laerer + "\n" + trindeling + "\n"
                + niveau;

        return information;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox holdCombo;
    private javax.swing.JTextArea infoArea;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JComboBox klassetrinCombo;
    private javax.swing.JComboBox laererCombo;
    private javax.swing.JTextField nytFag;
    private javax.swing.JComboBox opdelingCombo;
    private javax.swing.JButton opretHoldFag;
    private javax.swing.JComboBox vaelgFag;
    private javax.swing.JComboBox vaelgHoldCombo;
    // End of variables declaration//GEN-END:variables
}
